package org.edu.modules.sxjy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.sxjy.common.noresubmit.CacheLock;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.entity.VeCompany;
import org.edu.modules.sxjy.entity.VeCompanyUser;
import org.edu.modules.sxjy.entity.VePost;
import org.edu.modules.sxjy.service.CacheKeyGenerator;
import org.edu.modules.sxjy.service.IVeCompanyService;
import org.edu.modules.sxjy.service.IVeCompanyUserService;
import org.edu.modules.sxjy.service.IVePostService;
import org.edu.modules.sxjy.service.impl.CacheKeyGeneratorImp;
import org.edu.modules.sxjy.utils.PageResponseResult;
import org.edu.modules.sxjy.vo.VePostUpdateVo;
import org.edu.modules.sxjy.vo.VePostVo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

@Api(tags={"岗位管理"})
@RestController
@RequestMapping({"/sxjy/vePost"})
@ApiSort(60)
public class VePostController
        extends BaseController<VePost, IVePostService>
{
    private static final Logger log = LoggerFactory.getLogger(VePostController.class);
    @Autowired
    private IVePostService vePostService;
    @Autowired
    private IVeCompanyService veCompanyService;
    @Autowired
    private IVeCompanyUserService veCompanyUserService;

    @Bean
    public CacheKeyGenerator cacheKeyGenerator()
    {
        return new CacheKeyGeneratorImp();
    }

    @AutoLog("岗位管理-导入数据")
    @ApiOperation(value="导入数据", notes="岗位管理-导入数据")
    @RequestMapping(value={"/importExcel"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
    @CacheLock(prefix="redisLock.test", expire=1)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response)
    {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        Iterator localIterator = fileMap.entrySet().iterator();
        if (localIterator.hasNext())
        {
            Map.Entry<String, MultipartFile> entity = (Map.Entry)localIterator.next();
            MultipartFile file = (MultipartFile)entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(1);
            params.setHeadRows(1);
            params.setNeedSave(true);
            int size = 0;
            try
            {
                List<VePostVo> listSysCategorys = ExcelImportUtil.importExcel(file.getInputStream(), VePostVo.class, params);
                //lbh 2024/07/31 localObject1 >> Iterator localObject1
                for (Iterator localObject1 = listSysCategorys.iterator(); ((Iterator)localObject1).hasNext();)
                {
                    VePostVo vePostVo = (VePostVo)((Iterator)localObject1).next();
                    boolean b = this.vePostService.importExcel(vePostVo);
                    if (b) {
                        size++;
                    }
                }
                return Result.ok("文件导入成功！数据行数：" + size);
            }
            catch (Exception e)
            {
                Object localObject1;
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败：" + e.getMessage());
            }
            finally
            {
                try
                {
                    file.getInputStream().close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
    }

    @AutoLog("岗位管理-导出数据")
    @ApiOperation(value="导出数据", notes="岗位管理-导出数据")
    @PostMapping({"/exportXls"})
    public ModelAndView exportXls(@RequestBody VeCommon veCommon)
    {
        List<VePostVo> vePostsVo = this.vePostService.exportXls(veCommon);

        ModelAndView modelAndView = new ModelAndView(new JeecgEntityExcelView());

        modelAndView.addObject("fileName", "岗位管理信息");
        modelAndView.addObject("entity", VePostVo.class);
        LoginUser user = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        modelAndView.addObject("params", new ExportParams("岗位管理列表", "导出人:" + user.getRealname(), "导出信息"));
        modelAndView.addObject("data", vePostsVo);
        return modelAndView;
    }

    @AutoLog("岗位管理-获得企业名称")
    @ApiOperation(value="获得企业名称", notes="岗位管理-获得企业名称")
    @GetMapping({"/getFirmName"})
    public Result<?> getFirmName()
    {
        List<VeCompany> veCompanyList = this.veCompanyService.list();
        return Result.OK(veCompanyList);
    }

    @AutoLog("岗位管理-根据企业id获得企业指导教师名称")
    @ApiOperation(value="根据企业id获得企业指导教师名称", notes="岗位管理-根据企业id获得企业指导教师名称")
    @GetMapping({"/getTeacharName"})
    public Result<?> getTeacharName(int cpId)
    {
        List<VeCompanyUser> veCompanyUserList = this.veCompanyUserService.getTeacharNameByCpId(cpId);
        return Result.OK(veCompanyUserList);
    }

    @AutoLog("岗位管理-新增信息")
    @ApiOperation(value="新增信息", notes="岗位管理-新增信息")
    @PutMapping({"/add"})
    @CacheLock(prefix="redisLock.test", expire=1)
    public Result<?> add(@RequestBody VePostVo vePostVo)
    {
        if (vePostVo != null)
        {
            this.vePostService.add(vePostVo);
            return Result.OK("添加成功！");
        }
        return Result.error("添加失败");
    }

    @AutoLog("岗位管理-修改信息")
    @ApiOperation(value="修改信息", notes="岗位管理-修改信息")
    @PutMapping({"/edit"})
    @CacheLock(prefix="redisLock.test", expire=1)
    public Result<?> edit(@RequestBody VePostUpdateVo vePostUpdateVo)
    {
        if (vePostUpdateVo != null)
        {
            this.vePostService.edit(vePostUpdateVo);
            return Result.OK("编辑成功！");
        }
        return Result.error("编辑失败");
    }

    @AutoLog("岗位管理-编辑回显")
    @ApiOperation(value="编辑回显", notes="岗位管理-编辑回显")
    @GetMapping({"/editEcho"})
    public Result<?> editEcho(int id)
    {
        if (id != 0)
        {
            VePostUpdateVo vePostUpdateVo = this.vePostService.editEcho(id);
            return Result.OK(vePostUpdateVo);
        }
        return Result.error("编辑回显失败");
    }

    @AutoLog("岗位管理-查询列表")
    @ApiOperation(value="查询列表", notes="岗位管理-查询列表")
    @GetMapping({"/selectAll"})
    public PageResponseResult selectAll(VePostVo vePostVo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, VeCommon veCommon)
    {
        Integer startLine = Integer.valueOf((pageNo.intValue() - 1) * pageSize.intValue());
        List<VePostVo> vePostsVo = this.vePostService.selectAll(vePostVo, startLine, pageSize, veCommon);
        List<VePostVo> total = this.vePostService.getSumPage(vePostVo, veCommon);
        if ((Objects.isNull(vePostsVo)) && (vePostsVo.size() <= 0)) {
            return new PageResponseResult(Integer.valueOf(1), 1, 0, Result.OK());
        }
        double totals = total.size();
        double pageSizes = pageSize.intValue();
        double totalPage = Math.ceil(totals / pageSizes);
        return new PageResponseResult(pageNo, (int)totalPage, total.size(), Result.OK(vePostsVo));
    }

    @AutoLog("岗位管理-通过id查询")
    @ApiOperation(value="通过id查询", notes="岗位管理-通过id查询")
    @GetMapping({"/queryById"})
    public Result<?> queryById(@RequestParam(name="id", required=true) String id)
    {
        VePost vePost = (VePost)this.vePostService.getById(id);
        if (vePost == null) {
            return Result.OK();
        }
        return Result.OK(vePost);
    }

    @AutoLog("岗位管理-通过id删除")
    @ApiOperation(value="通过id删除", notes="岗位管理-通过id删除")
    @DeleteMapping({"/delete"})
    public Result<?> delete(@RequestParam(name="id", required=true) String id)
    {
        this.vePostService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog("岗位管理-批量删除")
    @ApiOperation(value="批量删除", notes="岗位管理-批量删除")
    @DeleteMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids)
    {
        this.vePostService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @AutoLog("岗位管理-根据企业id获取岗位信息")
    @ApiOperation(value="根据企业id获取岗位信息", notes="岗位管理-根据企业id获取岗位信息")
    @GetMapping({"/getPostNameById"})
    public Result<?> getPostNameById(@RequestParam(name="id") int id)
    {
        return this.vePostService.getPostNameByCpId(id);
    }

    @AutoLog("岗位管理-根据企业id查询企业教师id")
    @ApiOperation(value="根据企业id查询企业教师id", notes="岗位管理-根据企业id查询企业教师id")
    @GetMapping({"/getTeacharIdByCpId"})
    public Result<?> getTeacharIdByCpId(@RequestParam(name="cpId") int cpId)
    {
        if (cpId != 0)
        {
            List<VeCompanyUser> veCompanyUserList = this.vePostService.getTeacharIdByCpId(cpId);
            return Result.OK(veCompanyUserList);
        }
        return Result.OK("获取失败，企业信息不存在");
    }

    @AutoLog("岗位管理-根据userId获取该用户的企业id")
    @ApiOperation(value="根据userId获取该用户的企业id", notes="岗位管理-根据userId获取该用户的企业id")
    @GetMapping({"/getPostCpIdByUserId"})
    public Result<?> getPostCpIdByUserId(String userType, String userId)
    {
        return this.vePostService.getPostCpIdByUserId(userType, userId);
    }
}
